﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Sto7a00Manager : Sto7a00Base
    {
        private static Sto7a00Manager _entity;

        public static Sto7a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sto7a00Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Sto7a00 model = null;
            Sto7a01 dtlModel = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            if ("A" == action) //新增
            {
                //保存概要表身
                model = new Sto7a00();
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                model.Tranid = Sys0a60Manager.Entity.CreateSn("sto7a00_listid", usercode);
                model.Trandocno = Sys0a60Manager.Entity.CreateSn("sto7a00_docno", usercode);
                model.Usercode = appstr;
                lst.Add(this.Add(model));

                foreach (DataRow dr in dtl.Rows)
                {
                    dtlModel = new Sto7a01();
                    Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);
                    dtlModel.Detailid = Sys0a60Manager.Entity.CreateSn("sto7a01_listid", usercode);
                    dtlModel.Ptranid = model.Tranid;

                    lst.Add(Sto7a01Manager.Entity.Add(dtlModel));
                }
            }
            else if ("M" == action)
            {
                model = this.GetModelById(Components.Validation.ToString(tb["tranid"]));
                if (null == model) return "该笔单据可能已经被删除！";
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                lst.Add(this.Update(model));

                foreach (DataRow dr in dtl.Rows)
                {
                    dtlModel = new Sto7a01();
                    Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);

                    if (!Components.Validation.IsNull(dr["detailid"]))
                    {
                        lst.Add(Sto7a01Manager.Entity.Modify(dtlModel));
                    }
                    else
                    {
                        dtlModel.Detailid = Sys0a60Manager.Entity.CreateSn("sto7a01_listid", usercode);
                        dtlModel.Ptranid = model.Tranid;

                        lst.Add(Sto7a01Manager.Entity.Add(dtlModel));
                    }
                }

                if (null != deldata)
                {
                    foreach (DataRow dr in deldata.Rows)
                    {
                        dtlModel = new Sto7a01();
                        dtlModel.Detailid = Components.Validation.ToString(dr["detailid"]);
                        if (Validation.IsNull(dtlModel.Detailid)) continue;

                        lst.Add(Sto7a01Manager.Entity.Delete(dtlModel));
                    }
                }
            }
            else if ("K" == action)
            {
                model = this.GetModelById(Components.Validation.ToString(tb["tranid"]));
                if (null == model) return "该笔单据可能已经被删除！";
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                lst.Add(this.Update(model));

                foreach (DataRow dr in dtl.Rows)
                {
                    lst.Add(sto7a00Dal.Send(Components.Validation.ToString(dr["detailid"]),
                        Components.Validation.ToDouble(dr["sendqty"])));
                }

                //更新订单状态(已发货)
                lst.Add(sto7a00Dal.AlreadySendDoc(Components.Validation.ToString(tb["tranid"])));
            }
            else if ("R" == action)
            {
                model = this.GetModelById(Components.Validation.ToString(tb["tranid"]));
                if (null == model) return "该笔单据可能已经被删除！";
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                lst.Add(this.Update(model));

                foreach (DataRow dr in dtl.Rows)
                {
                    lst.Add(sto7a00Dal.Recive(Components.Validation.ToString(dr["detailid"]),
                        Components.Validation.ToDouble(dr["recqty"])));
                }

                //更新订单状态(已发货)
                lst.Add(sto7a00Dal.AlreadyReciveDoc(Components.Validation.ToString(tb["tranid"])));
            }
            else if ("J" == action) //调整
            {
                model = this.GetModelById(Components.Validation.ToString(tb["tranid"]));
                model.Moduser = usercode; //调整人
                if (null == model) return "该笔单据可能已经被删除！";
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                lst.Add(this.Update(model));

                foreach (DataRow dr in dtl.Rows)
                {
                    lst.Add(sto7a00Dal.Adjuest(Components.Validation.ToString(dr["detailid"]),
                        Components.Validation.ToDouble(dr["recqty"])));
                }
            }
            else if ("P" == action) //打印配货单设置状态
            {
                lst.Add(sto7a00Dal.AlreadyPrePintDoc(Components.Validation.ToString(tb["tranid"])));
            }

            return CommonManager.Entity.SaveData(lst);
        }

        #endregion
    }
}



